﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using SH_VBoxLibrary;

namespace SH_VBoxServiceTrayApp.WForms
{
    public partial class FormConfigOptions : Form
    {


        /// <summary>
        /// This helps me determine if a change has been done to the VM List.
        /// If so... Show a Msg Box that the List has been modified.
        /// And that the Service needs to be Restarted for Changes to take effect.
        /// </summary>
        public bool flagDataModified { get; set; }



        public FormConfigOptions()
        {
            InitializeComponent();
                   
            this.loadConfigData();
        }


       

        /// <summary>
        /// Prepares window by reading the data from the Config file.
        /// </summary>
        public void loadConfigData()
        { 
            textbox_VBoxManageLocation.ResetText();
            textBox_VMFolderLocation.ResetText();
            textBox_VMName.ResetText();
            listBox_VMs.Items.Clear();
            button_save.Enabled = false; //Enabled only after data modification.

            this.flagDataModified = false;


            textbox_VBoxManageLocation.Text = ConfigIO.vbmFolderPath;

            textBox_VMFolderLocation.Text = ConfigIO.vmsFolderPath;

            for (int i = 0; i < ConfigIO.VmList.Count;i++ )
            {
                listBox_VMs.Items.Add(ConfigIO.VmList[i].ToString());
            }
        }


        private void button_save_Click(object sender, EventArgs e)
        {
            //Rewrite the XML file according to my new data now.
            if (this.flagDataModified)
            {
                DialogResult dialogResult = MessageBox.Show("Are you sure you want to save your changes?", "Config has been modified!", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    //confirm changes.
                    ConfigIO.vbmFolderPath = textbox_VBoxManageLocation.Text;

                    ConfigIO.vmsFolderPath = textBox_VMFolderLocation.Text;

                    ConfigIO.VmList = listBox_VMs.Items.Cast<String>().ToList();

                    //saving ONLY if its a yes.
                    ConfigIO.saveConfig();

                    this.flagDataModified = false;

                    this.Hide();
                }
                else if (dialogResult == DialogResult.No)
                {
                    //nothing is done... since they dont want to save.
                }  
            }


           
            
        }


        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            //this.flagDataModified = false;
        }

        private void button_browseVBoxManage_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "Choose VBoxManage.exe Folder:";
            DialogResult userResult = folderBrowserDialog1.ShowDialog();

            //Only considered it modified if the value its actually different. and the "OK" button was pressed.
            if (textbox_VBoxManageLocation.Text != folderBrowserDialog1.SelectedPath && folderBrowserDialog1.SelectedPath != "" && userResult == DialogResult.OK)
            {
                textbox_VBoxManageLocation.Text = folderBrowserDialog1.SelectedPath;
                flagDataModified = true;
                button_save.Enabled = true;
            }

        }

        //WARNING: Take into acount this will be changed to TrayApp Location
        private void button_browseVMFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "Choose VirtualBox's Virtual Machine Folder:";
            folderBrowserDialog1.ShowDialog();

            DialogResult userResult = folderBrowserDialog1.ShowDialog();

            //Only considered it modified if the value its actually different. and the "OK" button was pressed.
            if (textBox_VMFolderLocation.Text != folderBrowserDialog1.SelectedPath && folderBrowserDialog1.SelectedPath != "" && userResult == DialogResult.OK)
            {
                textBox_VMFolderLocation.Text = folderBrowserDialog1.SelectedPath;
                flagDataModified = true;
                button_save.Enabled = true;
            }

        }

        /// <summary>
        /// Adds the Value entered in the TextBox for "VM Name" to
        /// the ListBox right next to it.
        /// And clears the TextBox after insertion.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_addVMFromList_Click(object sender, EventArgs e)
        {

            if (textBox_VMName.Text != "" || textBox_VMName.Text != string.Empty)
            {
                listBox_VMs.Items.Add(textBox_VMName.Text);
                textBox_VMName.Text = string.Empty;
                this.flagDataModified = true;
                button_save.Enabled = true;
            }
            else
            {
                MessageBox.Show("VM Name is empty.","Empty Name!");
            }


        }

        /// <summary>
        /// Removes the Selected Index From the ListBox 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_removeVMFromList_Click(object sender, EventArgs e)
        {
            if (listBox_VMs.SelectedIndex > -1)
            {
                listBox_VMs.Items.Remove(listBox_VMs.Items[listBox_VMs.SelectedIndex]);
                this.flagDataModified = true;
                button_save.Enabled = true;
            }
        }

   

      
    }
}
